package com.whjfxx.terminal.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by b.c on 2022/5/7 9:15
 */
@Component
public class RedisUtil {

    static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static <V> void set(String key, V v) {
        redisTemplate.opsForValue().set(key, v);
    }

    public static <V> void set(String key, V v, long expiry) {
        redisTemplate.opsForValue().set(key, v, expiry, TimeUnit.SECONDS);
    }

    public static <V> void set(String key, V v, long expiry, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, v, expiry, timeUnit);
    }

    public static boolean delete(String key) {
       return redisTemplate.delete(key);
    }

    @Nullable
    public static <V> Boolean setIfAbsent(String key, V v) {
        return redisTemplate.opsForValue().setIfAbsent(key, v);
    }

    @Nullable
    public static <V> Boolean setIfAbsent(String key, V v, long expiry) {
        return redisTemplate.opsForValue().setIfAbsent(key, v, expiry, TimeUnit.SECONDS);
    }

    @Nullable
    public static <V> Boolean setIfAbsent(String key, V v, long expiry, TimeUnit timeUnit) {
        return redisTemplate.opsForValue().setIfAbsent(key, v, expiry, timeUnit);
    }

    @Nullable
    public static <V> Boolean setIfPresent(String k, V v) {
        return redisTemplate.opsForValue().setIfPresent(k, v);
    }

    @Nullable
    public static <V> Boolean setIfPresent(String k, V v, long expiry) {
        return redisTemplate.opsForValue().setIfPresent(k, v, expiry, TimeUnit.SECONDS);
    }

    @Nullable
    public static <V> Boolean setIfPresent(String k, V v, long expiry, TimeUnit timeUnit) {
        return redisTemplate.opsForValue().setIfPresent(k, v, expiry, timeUnit);
    }

    public static <V> void multiSet(Map<String, ? extends V> hash) {
        redisTemplate.opsForValue().multiSet(hash);
    }

    @Nullable
    public static <V> Boolean multiSetIfAbsent(Map<String, ? extends V> hash) {
        return redisTemplate.opsForValue().multiSetIfAbsent(hash);
    }

    public static <V> void hset(String hk, String k, V v) {
        redisTemplate.opsForHash().put(hk, k, v);
    }

    @Nullable
    public static <V> V get(String k) {
        return (V) redisTemplate.opsForValue().get(k);
    }

    @Nullable
    public static <V> V getAndSet(String k, V v) {
        return (V) redisTemplate.opsForValue().getAndSet(k, v);
    }

    @Nullable
    public static <V> List<V> multiGet(Collection<String> keys) {
        return (List<V>) redisTemplate.opsForValue().multiGet(keys);
    }

    @Nullable
    public static <V> V hget(String hk, String k) {
        return (V) redisTemplate.opsForHash().get(hk, k);
    }

    public static Long incrIfAbsent(String key,long expiry) {
        if (redisTemplate.hasKey(key)) {
            expiry = redisTemplate.getExpire(key, TimeUnit.SECONDS);
        }
        Long incr = redisTemplate.opsForValue().increment(key);
        redisTemplate.expire(key, expiry, TimeUnit.SECONDS);
        return incr;
    }
}
